﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Windows.Media.Animation;
//using MyMoviesBrowserLibrary.MyMovies;

namespace MyVideoViewerManager
{
    /// <summary>
    /// Interaction logic for ChooseMovieImageWindow.xaml
    /// </summary>
    public delegate void ImageSelectedEvent(object sender, string imageUrl, ImageType imageType);
    public partial class ChooseMovieImageWindow : Window
    {
        public event ImageSelectedEvent ImageSelected;

        private string movieTitle;
        private string imdb;
        private ImageType imageType;
        private MyVideoViewerManagerBusiness bl;

        public ChooseMovieImageWindow(string movieTitle, string imdb, ImageType imageType)
        {
            InitializeComponent();
            this.movieTitle = movieTitle;
            this.imageType = imageType;
            this.imdb = imdb;

            bl = new MyVideoViewerManagerBusiness();

            selectButton.Click += new RoutedEventHandler(selectButton_Click);
            moviesListBox.MouseDoubleClick += new MouseButtonEventHandler(moviesListBox_MouseDoubleClick);
            cancelButton.Click += new RoutedEventHandler(cancelButton_Click);
            cancelButton2.Click += new RoutedEventHandler(cancelButton_Click);
            selectButton2.Click += new RoutedEventHandler(selectButton2_Click);
            imagesListBox.MouseDoubleClick += new MouseButtonEventHandler(imagesListBox_MouseDoubleClick);

            Loaded += new RoutedEventHandler(ChooseMovieImageWindow_Loaded);
        }

        void imagesListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            SelectImage();
        }

        void selectButton2_Click(object sender, RoutedEventArgs e)
        {
            SelectImage();
        }

        void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        void moviesListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            SelectMovie();
        }

        void selectButton_Click(object sender, RoutedEventArgs e)
        {
            SelectMovie();
        }

        void ChooseMovieImageWindow_Loaded(object sender, RoutedEventArgs e)
        {
            SearchByIMDB();
            
        }

        private void SelectMovie()
        {
            Movie movie = moviesListBox.SelectedValue as Movie;
            if (movie == null)
            {
                MessageBox.Show("Please select a movie.");
                return;
            }
            GetMovieInfo(movie);
        }

        private void SelectImage()
        {
            Movie movie = imagesListBox.SelectedValue as Movie;
            if (movie == null)
            {
                MessageBox.Show("Please select an image.");
                return;
            }

            if (ImageSelected != null)
                ImageSelected(this, movie.ImageUrl, imageType);
            Close();
        }

        void SearchByIMDB()
        {
            if (string.IsNullOrEmpty(imdb))
                SearchByTitle();
            List<Movie> movies = bl.SearchByIMDB(imdb);
            if (movies.Count == 0)
                SearchByTitle();
            else if (movies.Count == 1)
            {
                GetMovieInfo(movies[0]);
            }
            else
            {
                moviesListBox.ItemsSource = movies;
                ((Storyboard)this.Resources["ShowMovieListStoryBoard"]).Begin();                
            }
            
        }
        void SearchByTitle()
        {
            if (string.IsNullOrEmpty(movieTitle))
            {
                MessageBox.Show("The movie title was blank, cannot search on a blank title");
                Close();
            }
            List<Movie> movies = bl.SearchByTitle(movieTitle);
            if (movies.Count == 0)
            {
                MessageBox.Show("No images found for title.");
                Close();
            }
            else if (movies.Count == 1)
            {
                GetMovieInfo(movies[0]);
            }
            else
            {
                moviesListBox.ItemsSource = movies;
                ((Storyboard)this.Resources["ShowMovieListStoryBoard"]).Begin();
            }
        }

        void GetMovieInfo(Movie movie)
        {
            ((Storyboard)this.Resources["ShowLoadingStoryBoard"]).Begin();
            
            List<Movie> movies = bl.GetMovieInfo(movie,imageType);
            if (movies.Count == 0)
            {
                MessageBox.Show("Search returned no results");
                Close();
            }
            imagesListBox.ItemsSource = movies;
            ((Storyboard)this.Resources["ShowImagesStoryboard"]).Begin();
        }
        

    }
}
